<template>
	<div id="bg" class="w-h-100" style="box-sizing: border-box; overflow: hidden">
		<div class="w-h-100 ver-flex">
			<el-card class="box-card" shadow="never" :body-style="{ paddingBottom: '0' }">
				<search ref="searchRef" v-bind="evaluationSearch">
					<template #footer>
						<el-button type="primary" icon="ele-Search" @click="query"> {{ $t('qEh3zD5bC8bZb4bLvFe29') }} </el-button>
						<el-button icon="ele-Plus" type="primary" @click="add"> {{ $t('wnpevud10fwvwhDyynC4A') }} </el-button>
						<el-button icon="ele-Download" type="primary" @click="down"> {{ $t('tjpsapuRnb7E4MDf9i9') }} </el-button>
						<el-upload action="action" :limit="1" :show-file-list="false" accept=".xlsx" :http-request="uploadFile" style="margin-left: 10px; padding-top: 4px">
							<template #trigger>
								<el-button icon="ele-Upload" type="primary">{{ $t('v5eJ3w4xVv87pHzef9Ijh') }}</el-button>
							</template>
						</el-upload>
					</template>
				</search>
			</el-card>

			<el-card class="w-h-100 flex-1 box-card" style="margin-top: 16px" shadow="never" :body-style="{ paddingBottom: '0' }">
				<inscroll-table :data-list="list" v-bind="evaluationTableConfig" :dataCount="Number(totalRows)" :handle-size-change="handleSizeChange" :handle-current-change="handleCurrentChange">
					<template #fileCount="scope">
						<div>
							{{ scope.row.v_FilePath ? scope.row.v_FilePath.length : 0 }}
						</div>
					</template>
					<template #operation="scope">
						<div>
							<el-button
								icon="ele-Document"
								@click="
									() => {
										detailAction(scope.row);
									}
								"
								type="primary"
								size="small"
								text
							>
								{{ $t('ucC9qxHvkvdh9OGEo1pn') }}
							</el-button>
							<el-button
								icon="ele-Edit"
								@click="
									() => {
										changeAction(scope.row);
									}
								"
								type="primary"
								size="small"
								text
							>
								{{ $t('kiUkiqa3h0HLymgh7eEb') }}
							</el-button>
							<el-button
								icon="ele-Delete"
								@click="
									() => {
										deleteAction(scope.row);
									}
								"
								type="danger"
								size="small"
								text
							>
								{{ $t('mkwlMbm3qr88f0XxV9Pje') }}
							</el-button>
						</div>
					</template>
				</inscroll-table>
			</el-card>
		</div>
		<evaluation-form ref="formRef" @submit="(param: any) => submitHandle(param)"></evaluation-form>
	</div>
</template>

<script lang="ts" setup>
import { inscrollTable, evaluationTableConfig } from './config/table/index';
import { search, evaluationSearch } from './config/search/index';
import evaluationForm from './component/add-edit-evaluation.vue';
import getStaticTableData from '/@/utils/staticGetTableData';
import { getEvaluationList, postEvaluation, postChangeEvaluation, postDeleteEvaluation } from '/@/api/safety-play/evaluationAPI';
import { ref } from 'vue';
import { IPostEvaluationType } from '/@/types/ys/index';
import { ElMessageBox } from 'element-plus';
import { fileToBase64, downUrl } from '/@/utils/hook/index';
import { postExcel } from '/@/api/safety-play/discernAPI';
import commonRequest from '/@/api/common/common';
import eventBus from '/@/utils/eventBus';
import { uploadFiles } from '/@/utils/hook/index';

import useCurrentInstance from '/@/utils/hook/useCurrentInstance';
const { proxy } = useCurrentInstance();
const $t = proxy.$t;

const { list, totalRows, handleSizeChange, handleCurrentChange, getData } = getStaticTableData(getEvaluationList, {}, evaluationSearch.searchObj);

const formRef = ref();
let isChange = false;
let currentSelectData: any;
//附件删除后刷新列表
eventBus.on('getData', getData);
const down = () => {
	downUrl(window.$host + '/Upload/SMS/Batch/LegalEvaluate.xlsx');
};

const uploadFile = (params: any) => {
	fileToBase64(params.file)
		.then(async (base64) => {
			console.log(base64); // 输出文件的Base64编码
			await postExcel({ ModelType: 'LegalEvaluate', FileContent: base64 });
			getData();
		})
		.catch((error) => {
			console.error(error); // 输出错误信息
		});
};

const add = () => {
	isChange = false;
	formRef.value.addNew({});
};
const detailAction = (data: any) => {
	isChange = false;
	formRef.value.detail(data);
};
const changeAction = (data: any) => {
	currentSelectData = data;
	isChange = true;
	formRef.value.change(data);
};
const deleteAction = (data: any) => {
	ElMessageBox.confirm($t('itYcbTIj20MmWuPdBMgvd'), $t('nHdM3UcfLoZbHyqgSa3cx'), {
		confirmButtonText: $t('cpmrq09Mw3tfVmwWGsZzb'),
		cancelButtonText: $t('wFa44E4Iz025Ha2gYeDop'),
		type: 'warning',
		center: true,
	})
		.then(async () => {
			await postDeleteEvaluation({
				n_Id: data.n_Id,
			});
			getData();
		})
		.catch(() => {});
};

const query = () => {
	getData();
};

const submitHandle = async (param: any) => {
	let tempFileID = 0;
	let res = await commonRequest.getGenerateId();
	tempFileID = res.data; //雪花id
	const postData: IPostEvaluationType = {
		V_Legal_Name: param.v_Legal_Name,
		V_Terms_Of_Use: param.v_Terms_Of_Use,
		V_Legal_Evluate: param.v_Legal_Evluate,
		V_Legal_Requirement: param.v_Legal_Requirement,
		V_Company_Status_Description: param.v_Company_Status_Description,
		V_Remake: param.v_Remake,
		N_DocumentId: tempFileID,
	};
	if (formRef.value.fileList.length > 0) {
		await uploadFiles(tempFileID, formRef.value.fileList);
	}
	if (isChange) {
		postData.n_Id = currentSelectData.n_Id;
		await postChangeEvaluation(postData);
	} else {
		await postEvaluation(postData);
	}
	getData();
};
</script>

<!-- scoped属性使style有自己独立的作用域(属性选择器实现 title[data-v-xxx]) -->
<style lang="scss" scoped>
//ys基本样式
#bg {
	:deep(.el-card__body) {
		height: 100%;
	}
}
</style>
